package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.dto.*;
import com.sky.entity.Inheritors;
import com.sky.entity.Nonheritage;
import com.sky.entity.NonheritageImg;
import com.sky.entity.Panorama;
import com.sky.mapper.InheritorsMapper;
import com.sky.mapper.NonheritageImgMapper;
import com.sky.mapper.PanoramaMapper;
import com.sky.service.NonheritageService;
import com.sky.mapper.NonheritageMapper;
import com.sky.vo.NonHeritagePageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author <a href="https://gitee.com/TZ0801/IntangibleCluturalHeritage">非遗</a>
* @description 针对表【nonheritage(非遗信息表)】的数据库操作Service实现
* @createDate 2024-09-28 17:30:15
*/
@Service
@Slf4j
public class NonheritageServiceImpl extends ServiceImpl<NonheritageMapper, Nonheritage>
    implements NonheritageService{

    @Autowired
    private InheritorsMapper inheritorsMapper;

    @Autowired
    private NonheritageImgMapper nhImgMapper;

    @Autowired
    private PanoramaMapper panoramaMapper;

    //TODO 开启事物
    @Override
    public boolean saveOrUpdate(NonHeritageDTO entity) {
        Nonheritage nonheritage = new Nonheritage();
        BeanUtils.copyProperties(entity, nonheritage);

        //向非遗表插入数据
        boolean bool= super.saveOrUpdate(nonheritage);

        //获取insert语句生成的非遗项目主键值
        Integer nonheritageId = nonheritage.getId();

        //向传承人表插入数据
        List<InheritorsDTO> inheritorsDTOList = entity.getInheritors();
        log.info("inheritors= {}", inheritorsDTOList);

        if (!inheritorsDTOList.isEmpty()) {
            List<Inheritors> inheritorsList = new ArrayList<>();

            for (InheritorsDTO dto : inheritorsDTOList) {
                Inheritors inheritor = new Inheritors(); // 假设Inheritors有一个无参构造函数
                BeanUtils.copyProperties(dto, inheritor); // 复制单个对象的属性
                inheritor.setNonheritageId(nonheritageId);
                inheritorsList.add(inheritor); // 将复制后的对象添加到列表中
            }

            log.info("{}", inheritorsList);
            inheritorsMapper.insert(inheritorsList);
        }


        //向非遗图表插入数据
        List<NonheritageImgDTO> imgDTOs = entity.getNonheritageImgs();
        if (!imgDTOs.isEmpty()) {
            List<NonheritageImg> imgs = new ArrayList<>();

            for (NonheritageImgDTO dto : imgDTOs) {
                NonheritageImg img = new NonheritageImg();
                BeanUtils.copyProperties(dto, img); // 复制单个对象的属性
                img.setNonheritageId(nonheritageId);
                imgs.add(img); // 将复制后的对象添加到列表中
            }
            log.info("{}", imgs);
            nhImgMapper.insertOrUpdate(imgs);
        }

        //向全景图表插入数据
        List<PanoramaDTO> panoramaDTOS = entity.getPanoramas();
        if (!panoramaDTOS.isEmpty()) {
            List<Panorama> panoramas = new ArrayList<>();

            for (PanoramaDTO dto : panoramaDTOS) {
                Panorama panorama = new Panorama();
                BeanUtils.copyProperties(dto, panorama); // 复制单个对象的属性
                panorama.setNonheritageId(nonheritageId);
                panoramas.add(panorama); // 将复制后的对象添加到列表中
            }
            log.info("{}", panoramas);
            panoramaMapper.insertOrUpdate(panoramas);
        }
        return bool;
    }

    @Override
    public boolean removeByIds(List<String> ids) {
        boolean bool = super.removeByIds(ids);

        for (String id : ids) {
            // 设置一个 columnMap，设置删除条件为 nonheritage_id = id
            Map<String, Object> columnMap = new HashMap<>();
            columnMap.put("nonheritage_id", id);

            inheritorsMapper.deleteByMap(columnMap);
            nhImgMapper.deleteByMap(columnMap);
            panoramaMapper.deleteByMap(columnMap);
        }
        return bool;
    }

    //TODO 分页查询
    @Override
    public NonHeritagePageVO pageQuery(NonHeritagePageDTO nonHeritagePageDTO) {
        //PageHelper.startPage
        Page<NonHeritagePageVO> pages = new Page<>();

        return null;
    }
}




